﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Isis.Interfaces;
using Isis.Views;


namespace Isis.Views
{
    public class PageLayoutWriter : IPageLayoutWriter
    {
        private readonly string _currentTabName;
        private readonly IControllerRepository _controllerRepository;
        private readonly string _basePath;
        private readonly string _currentSubTabUrlFragment;
        public ControllerRepository controllerRepository;
        private IEnumerable<string> SubTabs;
        SubTabWriter subTabWriter = new SubTabWriter();

        public PageLayoutWriter(string currentTabName, string currentSubTabUrlFragment, IControllerRepository controllerRepository,
            string basePath)
        {
            _currentTabName = currentTabName;
            _currentSubTabUrlFragment = currentSubTabUrlFragment;
            _controllerRepository = controllerRepository;
            _basePath = basePath;
        }

        public void RenderPage(TextWriter writer, Action<TextWriter> renderContent)
        {
            writer.Write("<html>");
            RenderHead(writer);
            RenderBody(writer, renderContent);
            writer.Write("</html>");
        }

        private void RenderHead(TextWriter writer)
        {
            writer.Write("<head><title>Control Panel : " + _currentTabName + "</title>");
            RenderStyle(writer);
            writer.Write("</head>");
        }

        private void RenderFooter(TextWriter writer)
        {
            writer.Write("<div class=\"footer\"><p>");
            writer.Write("&copy;<a href=\"http://nimblepros.com\">NimblePros</a>");
            writer.Write("</p></div>");
        }



        private void RenderStyle(TextWriter writer)
        {
            writer.WriteLine("<link type=\"text/css\" rel=\"stylesheet\" href=\"{0}/stylesheet\"></link>", _basePath);
        }

        private void RenderBody(TextWriter writer, Action<TextWriter> action)
        {
            writer.Write("<body><div class=\"wrapper\">");
            writer.Write("<h1>ASP.NET Control Panel</h1>");
            writer.Write("<div id=\"npBanner\"><span>Brought to you by</span> <a href=\"http://nimblepros.com/\">");
            writer.Write("<img src=\"{0}/npBrandingLogo\" alt=\"NimblePros\" />", _basePath);
            writer.Write("</a> <span>Agile Software Development. <i>Delivered</i>.</span></div>");
            RenderTabs(writer);
            writer.Write("<div id=\"main\"><div id=\"contents\">");
            action.Invoke(writer);
            writer.Write("</div>"); // contents
            writer.Write("</div>"); // main
            writer.Write("<div class=\"push\"></div>");
            writer.Write("</div>"); // wrapper
            RenderFooter(writer);
            writer.Write("</body>");
        }

        private void RenderTabs(TextWriter writer)
        {
            IEnumerable<string> controllerTabNames = _controllerRepository.GetControllerTabNames();
            writer.Write(@"
<div id=""header"">
	<ul id=""primary"">");
            foreach (string tabName in controllerTabNames)
            {
                RenderTab(writer, tabName);
            }
            //RenderTab(writer, "Home");
            //RenderTab(writer, "Files");
            //RenderTab(writer, "Trace");
            //RenderTab(writer, "Server");
            //RenderTab(writer, "Application");
            //RenderTab(writer, "Session");
            //RenderTab(writer, "Cache");
            //RenderTab(writer, "Logs");
            writer.Write(@"
	</ul>
	</div>
");
        }
       
        
        public void RenderTab(TextWriter writer, string tabName)
        {
            if (tabName == _currentTabName)
            {
                IEnumerable<string> controllerSubTabNames = _controllerRepository.GetControllerSubTabNames(tabName);

                writer.Write(@"<li><span>{0}</span>{1}</li>", tabName, subTabWriter.RenderSubTabs(_basePath, tabName, controllerSubTabNames, writer, _currentSubTabUrlFragment));

            }
            else
            {
                writer.Write(@"<li><a href=""{0}/{1}"">{2}</a></li>", _basePath, tabName.ToLower(), tabName);
            }
        }
    }
}